package ciel.menu;

import org.mt4j.components.visibleComponents.shapes.MTRectangle;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.util.MTColor;
import org.mt4j.util.font.FontManager;
import org.mt4j.util.font.IFont;
import org.mt4j.util.math.Vector3D;

import processing.core.PApplet;
import ciel.debugging.CielDebugger;
import ciel.res.CielSettings;
import ciel.scene.CielImageScene;

public class CielGlobalDecision extends MTRectangle{
	
	public static final int MODE_UPLOAD = 0;
	public static final int MODE_RESET = 1;
	public static final int MODE_SHUTDOWN = 2;
	
	private PApplet pApplet;
	private CielImageScene scene;
	private MTTextArea text;

	public CielGlobalDecision(CielImageScene scene, PApplet pApplet, float width, float height, int mode) {
		super(pApplet, width, height);
		this.pApplet = pApplet;
		this.scene = scene;
		initLayout();
		initInformation(mode);
		initConfirmButton(mode);
	}
	
	public void initLayout() {
		setHeightXYGlobal(CielSettings.Dimensions.SCREEN_HEIGHT);
		setWidthXYGlobal(CielSettings.Dimensions.SCREEN_WIDTH);
		setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH/2,CielSettings.Dimensions.SCREEN_HEIGHT/2));
		setEnabled(false);
		setNoStroke(true);
		setFillColor(new MTColor(125, 125, 125, 200));
	}
	
	public void initInformation(int mode) {
		text = new MTTextArea(pApplet);
		switch(mode) {
			case MODE_UPLOAD:
				text.setText("Sollen die annotierten Bilder jetzt veröffentlicht werden?");
				break;
			case MODE_RESET:
				text.setText("Sollen alle Bilder geschlossen werden?");
				break;
			case MODE_SHUTDOWN:
				text.setText("Soll das Programm beendet werden? Nicht exportierte Änderungen gehen verloren!");
			default:
				break;
		}
		text.setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH/2, CielSettings.Dimensions.SCREEN_HEIGHT/3));
		text.setEnabled(false);
		text.setFillColor(new MTColor(100,100,100, 220));
		text.setNoStroke(true);
		text.setFontColor(MTColor.WHITE);
		addChild(text);
	}
	
	public void initConfirmButton(int mode) {
		final CielGlobalDecision thisDecision = this;
		final MTTextArea nein = new MTTextArea(pApplet);
		final MTTextArea ja = new MTTextArea(pApplet);
		IFont font = FontManager.getInstance().createFont(pApplet, "arial.ttf", 50, new MTColor(255, 255, 255, 255));
		
		switch(mode) {
			case MODE_UPLOAD:
				ja.setFont(font);
				ja.setText("JA");
				ja.setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH/4, CielSettings.Dimensions.SCREEN_HEIGHT/2));
				ja.removeAllGestureEventListeners();
				ja.setFillColor(new MTColor(100,100,100, 220));
				ja.setNoStroke(true);
				
				ja.removeAllGestureEventListeners();
		        ja.registerInputProcessor(new TapProcessor(pApplet));
				ja.addGestureListener(TapProcessor.class, new IGestureEventListener() {
					public boolean processGestureEvent(MTGestureEvent ge) {	
						TapEvent te = (TapEvent)ge;
						if(te.isTapDown()) {
							text.setText("Bilder werden exportiert und hochgeladen ...");
							ja.setVisible(false);
							nein.setVisible(false);
							scene.exportHTML();
							thisDecision.setVisible(false);
							thisDecision.removeFromParent();
						}
						return false;
					}
				});
				
				addChild(ja);
				break;
			case MODE_SHUTDOWN:
				ja.setFont(font);
				ja.setText("JA");
				ja.setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH/4, CielSettings.Dimensions.SCREEN_HEIGHT/2));
				ja.removeAllGestureEventListeners();
				ja.setFillColor(new MTColor(100,100,100, 220));
				ja.setNoStroke(true);
				
				ja.removeAllGestureEventListeners();
		        ja.registerInputProcessor(new TapProcessor(pApplet));
				ja.addGestureListener(TapProcessor.class, new IGestureEventListener() {
					public boolean processGestureEvent(MTGestureEvent ge) {	
						TapEvent te = (TapEvent)ge;
						if(te.isTapDown()) {
							System.exit(0);
						}
						return false;
					}
				});
				
				addChild(ja);
				break;
			case MODE_RESET:
				ja.setFont(font);
				ja.setText("JA");
				ja.setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH/4, CielSettings.Dimensions.SCREEN_HEIGHT/2));
				ja.removeAllGestureEventListeners();
				ja.setFillColor(new MTColor(100,100,100, 220));
				ja.setNoStroke(true);
				
				ja.removeAllGestureEventListeners();
		        ja.registerInputProcessor(new TapProcessor(pApplet));
				ja.addGestureListener(TapProcessor.class, new IGestureEventListener() {
					public boolean processGestureEvent(MTGestureEvent ge) {	
						TapEvent te = (TapEvent)ge;
						if(te.isTapDown()) {
							scene.removeAllImagesFromCanvas();
							thisDecision.setVisible(false);
							thisDecision.removeFromParent();
						}
						return false;
					}
				});
				
				addChild(ja);
				break;
			default:
				break;
		}

		nein.setFont(font);
		nein.setText("NEIN");
		nein.setPositionGlobal(new Vector3D(CielSettings.Dimensions.SCREEN_WIDTH - CielSettings.Dimensions.SCREEN_WIDTH/4, CielSettings.Dimensions.SCREEN_HEIGHT/2));
		nein.setFillColor(new MTColor(100,100,100, 220));
		nein.setNoStroke(true);
		
		nein.removeAllGestureEventListeners();
        nein.registerInputProcessor(new TapProcessor(pApplet));
        nein.addGestureListener(TapProcessor.class, new IGestureEventListener() {
                public boolean processGestureEvent(MTGestureEvent ge) {
                        TapEvent te = (TapEvent)ge;
                        if(te.isTapDown()) {
                        	thisDecision.setVisible(false);
                        	thisDecision.removeFromParent();
                        	return true;
                        }
                        return false;
                }
        });
		
		
		addChild(nein);
	}

	

}
